<template>
  <div class="dict-page" v-loading="loading" element-loading-text="加载中...">
    <div class="dict-title">
      <div class="dict-title-left">
        <el-button type="primary" @click="goback">
          <template #icon><IEpBack /> </template>返回
        </el-button>
      </div>
      <div class="dict-title-right">
        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">
          添加
        </el-button>
      </div>
    </div>
    <div class="dict-table">
      <el-table
        border
        stripe
        :data="tableData"
        :height="store.tableHeight"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'sub', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="字典标签" />
        <el-table-column prop="code" label="字典类型" />
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="remark" label="备注说明" />
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column prop="created_user" label="创建人" />

        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="updateDeptFun('update', row)"
            >
              编辑
            </el-button>

            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <addDialog
    :dictId="dictId"
    :treeData="tableData"
    ref="deptAddRef"
    @refreshList="refreshList"
  />
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import { listApi, delApi } from "@/plugins/api/system/dict";
import addDialog from "./components/DialogDict.vue";
const store = useCounterStore();
const router = useRouter();
const route = useRoute();
let tableData = ref([]);
let loading = ref(false);

function goback() {
  router.back();
}
let dictId = ref(null);
function getData() {
  loading.value = true;

  listApi({
    dict_id: dictId.value,
  })
    .then((data) => {
      tableData.value = data;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let deptAddRef = ref(null);
function addDeptFun(type) {
  deptAddRef.value.initFun(type);
}
function updateDeptFun(type, row) {
  deptAddRef.value.initFun(type, row);
}
function refreshList() {
  getData();
}

function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id,
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}

onMounted(() => {
  if (route.query.dictId) {
    dictId.value = parseInt(route.query.dictId);
    getData();
  } else {
    router.back();
  }
});
</script>
<style scoped lang="scss">
.dict-page {
  width: 100%;
  height: 100%;

  .dict-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .dict-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      img {
        vertical-align: middle;
      }

      span {
        vertical-align: middle;
        margin-left: 5px;
      }
    }
  }

  .dict-table {
    padding: 10px;
    box-sizing: border-box;
  }
}
</style>
